package com.xdf.docmanagementsystem.service.impl;

import com.xdf.docmanagementsystem.common.base.BaseService;
import com.xdf.docmanagementsystem.common.util.Example;
import com.xdf.docmanagementsystem.common.util.StringUtil;
import com.xdf.docmanagementsystem.dao.DocumentMapper;
import com.xdf.docmanagementsystem.entity.Document;
import com.xdf.docmanagementsystem.entity.EmpExtraFile;
import com.xdf.docmanagementsystem.enums.PathEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.xdf.docmanagementsystem.entity.DocExtraFile;

import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.xdf.docmanagementsystem.dao.DocExtraFileMapper;
import com.xdf.docmanagementsystem.service.DocExtraFileService;

/**
 * @Description : com.xdf.docmanagementsystem.service.impl(业务层实现类,该类为生成器自动生成).
 * @Author : ZK
 * @Date : 2020-06-23 11:00:30
 */
@Service
public class DocExtraFileServiceImpl extends BaseService implements DocExtraFileService {
    @Autowired
    private DocExtraFileMapper dao;
    @Autowired
    private DocumentMapper documentMapper;

    /**
     * @Describe 新增方法
     * @author ZK
     * @Date 2020-06-23 11:00:30
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(DocExtraFile record) throws Exception {
        return dao.insert(record);
    }

    /**
     * @Describe 选择新增
     * @author ZK
     * @Date 2020-06-23 11:00:30
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertSelective(DocExtraFile record) throws Exception {
        return dao.insertSelective(record);
    }

    /**
     * @Describe 批量新增
     * @author ZK
     * @Date 2020-06-23 11:00:30
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int multiInsert(List<DocExtraFile> list) throws Exception {
        int count = 0;
        String savePath = new StringBuilder()
                .append(PathEnum.SAVE_PATH.getPath())
                .append(File.separator)
                .append(PathEnum.DOC_SUB_PATH.getPath())
                .append(File.separator).toString();
        for (DocExtraFile docExtraFile : list) {
            Document document = documentMapper.selectByPrimaryKey(docExtraFile.getDocumentId());
            docExtraFile.setCreateUserId(getUserId());
            docExtraFile.setFilePath(savePath
                    + document.getId()
                    + File.separator
                    + docExtraFile.getFileName()
            );
            docExtraFile.setFileType(StringUtil.getFileExtension(docExtraFile.getFileName()));
            count += dao.insertSelective(docExtraFile);
        }
        return count;
    }

    /**
     * @Describe 修改
     * @author ZK
     * @Date 2020-06-23 11:00:30
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateByPrimaryKey(DocExtraFile record) throws Exception {
        return dao.updateByPrimaryKey(record);
    }

    /**
     * @Describe 选择修改
     * @author ZK
     * @Date 2020-06-23 11:00:30
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateByPrimaryKeySelective(DocExtraFile record) throws Exception {
        return dao.updateByPrimaryKeySelective(record);
    }

    /**
     * @Describe 修改
     * @author ZK
     * @Date 2020-06-23 11:00:30
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int update(DocExtraFile record) throws Exception {
        return dao.updateByPrimaryKey(record);
    }

    /**
     * @Describe 批量修改
     * @author ZK
     * @Date 2020-06-23 11:00:30
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int multiUpdate(List<DocExtraFile> list) throws Exception {
        int count = 0;
        if (list == null || list.size() <= 0) return 0;
        Integer documenId = list.stream().filter(x -> x.getDocumentId() != null)
                .map(x -> x.getDocumentId())
                .findFirst().orElse(null);
        List<DocExtraFile> dtl = dao.selectByExample(new Example().andEq("document_id", documenId)
                .andEq("file_status", 1));
        List<Integer> i = list.stream().filter(x -> x.getId() != null)
                .map(x -> x.getId())
                .collect(Collectors.toList());
        for (DocExtraFile docExtraFile : dtl) {
            if (!i.contains(docExtraFile.getId())) {
                dao.updateByPrimaryKeySelective(DocExtraFile.builder().fileStatus(0).id(docExtraFile.getId()).build());
            }
        }

        for (DocExtraFile docExtraFile : list) {
            if (docExtraFile.getId() != null) {
                count += dao.updateByPrimaryKeySelective(docExtraFile.builder()
                        .id(docExtraFile.getId())
                        .fileName(docExtraFile.getFileName())
                        .remarks(docExtraFile.getRemarks()).build());
            } else {
                String savePath = new StringBuilder()
                        .append(PathEnum.SAVE_PATH.getPath())
                        .append(File.separator)
                        .append(PathEnum.DOC_SUB_PATH.getPath())
                        .append(File.separator).toString();
                docExtraFile.setUploadTime(new Date());
                docExtraFile.setCreateTime(new Date());
                docExtraFile.setFileType(StringUtil.getFileExtension(docExtraFile.getFileName()));
                docExtraFile.setFilePath(savePath
                        + documenId
                        + File.separator
                        + docExtraFile.getFileName());
                count += dao.insertSelective(docExtraFile);
            }
        }

        return count;
    }

    /**
     * @Describe 根据主键查询
     * @author ZK
     * @Date 2020-06-23 11:00:30
     */
    @Override
    public DocExtraFile selectByPrimaryKey(Integer id) throws Exception {
        return dao.selectByPrimaryKey(id);
    }

    /**
     * @Describe 根据主键删除
     * @author ZK
     * @Date 2020-06-23 11:00:30
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteByPrimaryKey(Integer id) throws Exception {
        return dao.deleteByPrimaryKey(id);
    }

    /**
     * @Describe 根据Entity查询
     * @author ZK
     * @Date 2020-06-23 11:00:30
     */
    @Override
    public List<DocExtraFile> listQuery(DocExtraFile record) throws Exception {
        return dao.selectByParam(record);
    }

}